layui.use(['table', 'HttpRequest', 'func', 'xmSelect'], function () {

    var $ = layui.$;
    var table = layui.table;
    var HttpRequest = layui.HttpRequest;
    var func = layui.func;
    var xmSelect = layui.xmSelect;
    var tradePersonXmSel;
    var goodsXmSel;

    // 初始化组织树
    new HttpRequest(Feng.ctxPath + "/userPayBillsDetail/getTradePersonList", 'get', function (data) {

        var tradePersonList = data.data;
        var tradePersonXmSelData = [];
        for (let i = 0; i < tradePersonList.length; i++) {
            tradePersonXmSelData.push({name: tradePersonList[i], value: tradePersonList[i]});
        }


        tradePersonXmSel = xmSelect.render({
            el: '#tradePerson',
            data: tradePersonXmSelData,
            toolbar: {
                show: true,
            },
            filterable: true,
            paging: true,
            pageSize: 6,

        });
    }).start();

    // 初始化组织树
    new HttpRequest(Feng.ctxPath + "/userPayBillsDetail/getGoodsList", 'get', function (data) {

        var goodsList = data.data;
        var goodsXmSelData = [];
        for (let i = 0; i < goodsList.length; i++) {
            goodsXmSelData.push({name: goodsList[i], value: goodsList[i]});
        }

        goodsXmSel = xmSelect.render({
            el: '#goods',
            data: goodsXmSelData,
            toolbar: {
                show: true,
            },
            filterable: true,
            paging: true,
            pageSize: 6,

        });
    }).start();


    /**
     * 用户支付账单详情管理
     */
    var UserPayBillsDetail = {
        tableId: "userPayBillsDetailTable"
    };

    /**
     * 初始化表格的列
     */
    UserPayBillsDetail.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'userId', sort: true, title: '用户Id'},
            {field: 'payBillsType', sort: true, title: '支付账单类型'},
            {field: 'tradeTime', sort: true, title: '交易时间'},
            {field: 'tradeType', sort: true, title: '交易类型'},
            {field: 'tradePerson', sort: true, title: '交易对方'},
            {field: 'goods', sort: true, title: '商品'},
            {field: 'amountType', sort: true, title: '金额类型'},
            {field: 'amount', sort: true, title: '金额'},
            {field: 'paymentWay', sort: true, title: '支付方式'},
            {field: 'currentStatus', sort: true, title: '当前状态'},
            {field: 'tradeNo', sort: true, title: '交易单号'},
            {field: 'merchantNo', sort: true, title: '商户单号'},
            {field: 'remarks', sort: true, title: '备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    UserPayBillsDetail.search = function () {
        var queryData = {};
        var tradePersonList = tradePersonXmSel.getValue('value');
        var goodsList = goodsXmSel.getValue('value');
        console.log(tradePersonXmSel.getValue('value'));
        queryData['goodsList'] = goodsList;
        queryData['tradePersonList'] = tradePersonList;
        table.reload(UserPayBillsDetail.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    UserPayBillsDetail.openAddDlg = function () {
        func.open({
            title: '添加用户支付账单详情',
            content: Feng.ctxPath + '/view/userPayBillsDetail/add',
            tableId: UserPayBillsDetail.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    UserPayBillsDetail.openEditDlg = function (data) {
        func.open({
            title: '修改用户支付账单详情',
            content: Feng.ctxPath + '/view/userPayBillsDetail/edit?id=' + data.id,
            tableId: UserPayBillsDetail.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    UserPayBillsDetail.exportExcel = function () {
        var checkRows = table.checkStatus(UserPayBillsDetail.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    UserPayBillsDetail.onDeleteItem = function (data) {
        var operation = function () {
            var request = new HttpRequest(Feng.ctxPath + "/userPayBillsDetail/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(UserPayBillsDetail.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            request.set("id", data.id);
            request.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + UserPayBillsDetail.tableId,
        url: Feng.ctxPath + '/userPayBillsDetail/findPage',
        method: 'post',
        page: true,
        cellMinWidth: 100,
        cols: UserPayBillsDetail.initColumn(),
        request: {pageName: 'pageNo', limitName: 'pageSize'},
        parseData: Feng.parseData,
        done: function(res, curr, count){
            $("[data-field = 'payBillsType']").children().each(function(){
                if($(this).text() == '1'){
                    $(this).text("支付宝支付");
                }else if($(this).text() == '0'){
                    $(this).text("微信支付");
                }
            })
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        UserPayBillsDetail.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        UserPayBillsDetail.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        UserPayBillsDetail.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + UserPayBillsDetail.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            UserPayBillsDetail.openEditDlg(data);
        } else if (layEvent === 'delete') {
            UserPayBillsDetail.onDeleteItem(data);
        }
    });
});
